//
//  UIViewController+List.m
//  KKListViewDemo
//
//  Created by liaozhenming on 16/11/4.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "UIViewController+List.h"
#import <objc/runtime.h>

static const void *UIViewControllerSectionItemsValue = &UIViewControllerSectionItemsValue;
static const void *UIViewControllerTableViewValue = &UIViewControllerTableViewValue;
static const void *UIViewControllerCollectionViewValue = &UIViewControllerCollectionViewValue;
static const void *UIViewControllerProtocolManagerValue = &UIViewControllerProtocolManagerValue;

@implementation UIViewController (List)

@dynamic kk_tableView;
- (void)setKk_tableView:(UITableView *)kk_tableView {
    objc_setAssociatedObject(self, UIViewControllerTableViewValue, kk_tableView, OBJC_ASSOCIATION_ASSIGN);
}

- (UITableView*)kk_tableView {
    return objc_getAssociatedObject(self, UIViewControllerTableViewValue);
}

@dynamic kk_collectionView;
- (void)setKk_collectionView:(UICollectionView *)kk_collectionView {
    objc_setAssociatedObject(self, UIViewControllerCollectionViewValue, kk_collectionView, OBJC_ASSOCIATION_ASSIGN);
}

- (UICollectionView*)kk_collectionView {
    return objc_getAssociatedObject(self, UIViewControllerCollectionViewValue);
}

@dynamic kk_sectionItems;
- (void)setKk_sectionItems:(NSMutableArray *)kk_sectionItems {
    objc_setAssociatedObject(self, UIViewControllerSectionItemsValue, kk_sectionItems, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    self.kk_listViewProtocolManager.kk_sectionItems = kk_sectionItems;
    //  刷新数据
    [self.kk_tableView reloadData];
    [self.kk_collectionView reloadData];
}

- (NSMutableArray*)kk_sectionItems {
    return objc_getAssociatedObject(self, UIViewControllerSectionItemsValue);
}

@dynamic kk_listViewProtocolManager;
- (void)setKk_listViewProtocolManager:(KKListViewProtocolManager *)kk_listViewProtocolManager {
    objc_setAssociatedObject(self, UIViewControllerProtocolManagerValue, kk_listViewProtocolManager, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (KKListViewProtocolManager*)kk_listViewProtocolManager {
    return objc_getAssociatedObject(self, UIViewControllerProtocolManagerValue);
}

#pragma mark - Public methods
//  注册tableView delegate如果为nil 则表示代理为基类处理
- (void)kk_registerTableView:(UITableView*)tableView delegate:(__weak id)delegate {
    [self setKk_tableView: tableView];
    
    if (delegate == nil) {
        KKListViewProtocolManager *manager = [KKListViewProtocolManager kk_listViewProtocolManagerWithSectionItems: self.kk_sectionItems];
        tableView.delegate = manager;
        tableView.dataSource = manager;
        [self setKk_listViewProtocolManager: manager];
    }
    
#warning 需要和KKListViewTools 一起配合使用
}

//  注册collectionView delegate如果为nil 则表示代理为基类处理
- (void)kk_registerCollectionView:(UICollectionView*)collectionView delegate:(__weak id)delegate {
    [self setKk_collectionView: collectionView];
    if (delegate == nil) {
        KKListViewProtocolManager *manager = [KKListViewProtocolManager kk_listViewProtocolManagerWithSectionItems: self.kk_sectionItems];
        collectionView.delegate = manager;
        collectionView.dataSource = manager;
        [self setKk_listViewProtocolManager: manager];
    }
    
#warning 需要和KKListViewTools 一起配合使用
}

@end

#pragma mark - About UITableView methods

@implementation KKTableViewCell

@end

@implementation KKTableViewHeaderView

@end

@implementation KKTableViewFooterView

@end

#pragma mark - About UICollectionView methods

@implementation KKCollectionViewCell

@end

@implementation KKCollectionHeaderView

@end

@implementation KKCollectionFooterView

@end
